package jnzb.ui.queue;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.Border;
import javax.swing.border.EtchedBorder;
import javax.swing.table.TableModel;

import jnzb.ui.table.JTableTT;
import jnzb.ui.table.TableColumnAdjuster;

public class FinishedPanel extends JPanel {

	private static final int FIXED_COL_SIZE = 60;

	private static final int COLUMNS_HIGH = 5;

	private JTable finishedTable;
	private TableColumnAdjuster tca;
	private JScrollPane scrollPane;

	public FinishedPanel() {
		create();
	}

	private void create() {
		Border etchedBorder = BorderFactory
				.createEtchedBorder(EtchedBorder.LOWERED);
		Border titleBorder = BorderFactory.createTitledBorder(etchedBorder,
				"Finished NZBs");
		this.setBorder(titleBorder);

		this.setLayout(new BorderLayout());

		// Create a new BOLD version of the current font
		Font boldFont = new Font(super.getFont().getName(), Font.BOLD, super
				.getFont().getSize());

		JLabel text = new JLabel(
				"The following downloads were completed during this session.");
		this.add(text, BorderLayout.NORTH);

		text.setHorizontalTextPosition(JLabel.LEFT);
		text.setHorizontalAlignment(JLabel.LEFT);

		Dimension size = text.getPreferredSize();
		size.setSize(size.getWidth(), size.getHeight() + 10);
		text.setPreferredSize(size);
		text.setMaximumSize(size);
		text.setMinimumSize(size);
		text.setSize(size);
		text.setFont(boldFont);

		// Create empty table
		finishedTable = new JTableTT();
		finishedTable.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
		
		// Set preferred viewport size to current width + set height
		Dimension currentPreferred = finishedTable
				.getPreferredScrollableViewportSize();

		// Show COLUMNS_HIGH colummns in the viewport
		Dimension preferredHeight = new Dimension();
		preferredHeight.setSize(currentPreferred.getWidth(), finishedTable
				.getRowHeight()
				* COLUMNS_HIGH);

		// Set preferred viewable size
		finishedTable.setPreferredScrollableViewportSize(preferredHeight);

		// Add table to scroll pane
		scrollPane = new JScrollPane(finishedTable);

		// We have a border on the panel, so remove the border on the scroll
		// panel
		scrollPane.setBorder(BorderFactory.createEmptyBorder());

		// Create height that is the same as the scroll panel + header +
		// instruction label height + padding
		Dimension panelHeight = new Dimension();
		panelHeight.setSize(preferredHeight.getWidth(), preferredHeight
				.getHeight()
				+ finishedTable.getRowHeight()
				+ text.getPreferredSize().getHeight() + 14);

		this.setPreferredSize(panelHeight);

		this.add(scrollPane, BorderLayout.CENTER);
	}

	/**
	 * Set the underlying model
	 * 
	 * @param tableModel
	 */
	public void setModel(TableModel tableModel) {
		this.finishedTable.setModel(tableModel);
		resizeColumns();
	}
	
	public void resizeColumns(){
		// TODO - might be better to be relative to preferred size
		this.finishedTable.getColumnModel().getColumn(0).setMaxWidth(FIXED_COL_SIZE);
		this.finishedTable.getColumnModel().getColumn(1).setMaxWidth(FIXED_COL_SIZE);
	}
}
